/*
 * Copyright (C) 2019 Peng fei Pan <panpfpanpf@outlook.me>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.panpf.sketch.state;

import me.panpf.sketch.SketchView;
import me.panpf.sketch.drawable.MiPixmapElement;
import me.panpf.sketch.drawable.SketchShapeBitmapDrawable;
import me.panpf.sketch.request.DisplayOptions;
import me.panpf.sketch.request.ShapeSize;
import me.panpf.sketch.shaper.ImageShaper;
import me.panpf.sketch.zoom.PixmapBuild;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.Element;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.Size;

import java.io.IOException;

/**
 * 给什么图片显示什么图片，支持 {@link ShapeSize} 和 {@link ImageShaper}
 */
public class DrawableStateImage implements StateImage {

    private MiPixmapElement originDrawable;
    private int resId = -1;

    public DrawableStateImage(MiPixmapElement drawable) {
        this.originDrawable = drawable;
    }

    public DrawableStateImage(int resId) {
        this.resId = resId;
    }

    @Override
    public MiPixmapElement getDrawable(Context context, SketchView sketchView, DisplayOptions displayOptions) {
        MiPixmapElement drawable = originDrawable;
        ImageSource imageSource = null;
        if (drawable == null && resId != -1) {
            Resource resource = null;
            try {
                resource = context.getResourceManager().getResource(resId);
                ImageSource.DecodingOptions decodingOptions = null;
                if (displayOptions.getShapeSize() != null) {
                    decodingOptions = new ImageSource.DecodingOptions();
                    decodingOptions.desiredSize = new Size(displayOptions.getShapeSize().getWidth(), displayOptions.getShapeSize().getHeight());
                }
                imageSource = ImageSource.create(resource, null);
                drawable = new MiPixmapElement(new PixmapBuild(imageSource,decodingOptions));
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NotExistException e) {
                e.printStackTrace();
            }
        }

        ShapeSize shapeSize = displayOptions.getShapeSize();
        ImageShaper imageShaper = displayOptions.getShaper();
        if ((shapeSize != null || imageShaper != null)) {
            drawable = new SketchShapeBitmapDrawable(context, drawable, shapeSize, imageShaper);
        }

        return drawable;
    }


    public Element getOriginDrawable() {
        return originDrawable;
    }

    public int getResId() {
        return resId;
    }
}
